﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

namespace ZX.BLL
{
    public class BhrCandidateHire : BaseBussnies
    {
        public override bool Insert(object entitys, out string msg)
        {
            bool rs = base.Insert(entitys, out msg);
            if (rs)
            {
                hrCandidateHire obj = entitys as hrCandidateHire;
                RunJob(obj);
            }
            return rs;
        }

        //public override bool Update(object entitys, out string msg)
        //{
        //    bool rs = base.Update(entitys, out msg);
        //    if (rs)
        //    {
        //        hrCandidateHire obj = entitys as hrCandidateHire;
        //        RunJob(obj,1);
        //    }
        //    return rs;
        //}

        //public override bool delete(object entitys, out string msg)
        //{
        //    bool rs= base.delete(entitys, out msg);
        //    if (rs)
        //    {
        //        hrCandidateHire obj = entitys as hrCandidateHire;
        //        RunJob(obj,2);
        //    }
        //    return rs;
        //}
        /// <summary>
        /// 业务逻辑处理
        /// </summary>
        /// <param name="obj">实体</param>
        /// <param name="function">逻辑功能0－新增，1－修改，2－删除</param>
        private void RunJob(hrCandidateHire obj)
        {
            //招聘选拔时更改选拔状态       
            try
            {
                string condition = string.Empty;
                ZX.BLL.Extend.BaseBussnies bll = null;
                BaseEntity Instance = null;
                Instance = ZX.Common.Globals.getEntity("hrCandidate") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(Instance);
                condition = string.Format("ID='{0}'", obj.CandidateID);
                hrCandidate hrcandidate = bll.GetEntityByCondition(condition) as hrCandidate;
                hrcandidate.HireStatus = 1; //录用
                bll.Update(hrcandidate);
                try
                {
                    //获取试用期限
                    ZX.SYS.Library.DatPhraseDetail Instance2 = ZX.Common.Globals.getEntity("DatPhraseDetail") as ZX.SYS.Library.DatPhraseDetail;
                    ZX.BLL.Extend.BaseBussnies DatPhraseDetailBll = new ZX.BLL.Extend.BaseBussnies(Instance2);
                    string condition2 = string.Format("PhraseID='{0}'", obj.ProbationPeriod.ToString());
                    ZX.SYS.Library.DatPhraseDetail employeeTypeEntity = DatPhraseDetailBll.GetEntityByCondition(condition2) as ZX.SYS.Library.DatPhraseDetail;
                    int QiXianValue = int.Parse(employeeTypeEntity.PhraseValue);
                    DateTime endDate = obj.JoinDate.AddMonths(QiXianValue);
                    if (obj.JoinDate <= DateTime.Now)//入职时间小于等于当前时间,立即入职
                    {
                        //将候选人信息插入到hrEmployee表中
                        ZX.EHR.Library.hrEmployee hrEmployeeInstance = ZX.Common.Globals.getEntity("hrEmployee") as ZX.EHR.Library.hrEmployee;
                        hrEmployeeInstance.ID = Guid.NewGuid();
                        hrEmployeeInstance.IDCardNO = hrcandidate.IDCardNo;//身份证号码
                        hrEmployeeInstance.CName = hrcandidate.Name;//姓名
                        hrEmployeeInstance.Gender = hrcandidate.Gender;//性别
                        hrEmployeeInstance.PhoneNo = hrcandidate.PhoneNO;//手机
                        hrEmployeeInstance.Email = hrcandidate.Email;
                        hrEmployeeInstance.DepartmentID = obj.DeptID;//部门
                        hrEmployeeInstance.PostID = obj.PostID;//岗位
                        hrEmployeeInstance.BirthDate = hrcandidate.BirthDay;//出生日期
                        hrEmployeeInstance.TopDegree = hrcandidate.Degree;//学历
                        hrEmployeeInstance.EmployeeStatus = 1;//在职
                        hrEmployeeInstance.JoinDate = obj.JoinDate;//入职日期
                        //如果试用期限加上入职日期<=当前日期，则为正式员工，否则为试用员工
                        if (endDate <= DateTime.Now) //正式
                        {
                            hrEmployeeInstance.EmployeeType = new Guid("D924C147-1A23-4FBE-A958-2F58FD9377D7");//正式员工
                        }
                        else
                        {
                            hrEmployeeInstance.EmployeeType = new Guid("8D7C895F-1DAD-4C72-A299-1EC9C9D13BC3");//试用员工
                        }
                        hrEmployeeInstance.AddTime = DateTime.Now;
                        hrEmployeeInstance.AddUser = obj.AddUser;
                        //获取员工工号
                        condition = string.Format("DeptID='{0}'", obj.DeptID);
                        DatDepartment _Dept = ZX.Common.Globals.getEntity("DatDepartment") as DatDepartment;
                        ZX.BLL.Extend.BaseBussnies DeptBll = new ZX.BLL.Extend.BaseBussnies(_Dept);
                        _Dept = DeptBll.GetEntityByCondition(condition) as DatDepartment;//获取该部门信息
                        //工号实体信息
                        BaseEntity instance = ZX.Common.Globals.getEntity("Sequence") as BaseEntity;
                        ZX.BLL.Extend.BaseBussnies SequenceBll = new ZX.BLL.Extend.BaseBussnies(instance);
                        Sequence sequence = SequenceBll.GetEntityByCondition<Sequence>("SequenceName='EmpCode'");
                        int numeLen = 0;
                        numeLen = sequence.NumLength;
                        //生成工号
                        Int64 num = IDMaker.GetSequenceNum("EmpCode");
                        string sequenceNum = num.ToString();
                        if (numeLen > num.ToString().Length)
                        {
                            for (int i = 0; i < numeLen - num.ToString().Length; i++)
                            {
                                sequenceNum = "0" + sequenceNum;
                            }
                        }
                        hrEmployeeInstance.EmployeeCode = _Dept.HelpCode + sequenceNum;//员工工号
                        ZX.BLL.Extend.BaseBussnies hrEmployeeBll = new ZX.BLL.Extend.BaseBussnies(hrEmployeeInstance);
                        hrEmployeeBll.Insert();
                    }
                }
                catch
                { }
            }
            catch (Exception e)
            { }
        }
    }
}
